NA for 1 particular date when converting dates from "character" format to "POSIXct" with as.POSIXct
NickName:Niltzable Ask DateTime:2020-02-28T02:11:46

NA for 1 particular date when converting dates from "character" format to "POSIXct" with as.POSIXct

I'm converting a string vector to date format with as.POSIXct(). Here is the strange thing:

as.POSIXct("2017-03-26 03:00:00.000",format="%Y-%m-%d %H")

#Gives

"2017-03-26 03:00:00 CEST"

#While

as.POSIXct("2017-03-26 02:00:00.000",format="%Y-%m-%d %H")

#Outputs
NA

This is really confusing and frustrating. It seem like the function really doesn't like the specific time: 02:00:00.000

Copyright Notice:Content Author:「Niltzable」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60439346/na-for-1-particular-date-when-converting-dates-from-character-format-to-posix

Answers
akrun 2020-02-27T18:12:57

We can specify the %T for time. In the format, there are minutes, seconds and millseconds. So, the %H is only matching the hour part\n\nas.POSIXct(\"2017-03-26 02:00:00.000\",format=\"%Y-%m-%d %T\")\n[1] \"2017-03-26 02:00:00 EDT\"\n\n\nOr to take care of the milliseconds as well\n\nas.POSIXct(\"2017-03-26 02:00:00.000\",format=\"%Y-%m-%d %H:%M:%OS\")\n#[1] \"2017-03-26 02:00:00 EDT\"\n\n\n\n\nOr using lubridate\n\nlibrary(lubridate)\nymd_hms(\"2017-03-26 02:00:00.000\")\n",


More about “NA for 1 particular date when converting dates from "character" format to "POSIXct" with as.POSIXct” related questions

The problem is, when converting into dates, the NULL values are replaced by NA

The dataset that I am trying to convert from factor to date in r program, has a column which contains NULL and Date values. I tried to convert as follows as.Date(data$Variable_42,format ...

Show Detail

Converting dates from excel to R

I have difficulty converting dates from excel (reading from csv) to R. Help is much appreciated. Here is what I'm doing: df$date = as.Date(df$excel.date, format = "%d/%m/%Y") However, some dat...

Show Detail

as.Date() changing the values of dates to NA

I am trying to change these dates from character to dates using the as.Date function as such: dates<-c("2018-01", "2018-02", "2018-03", "2018-04", "2...

Show Detail

Getting mysterious NA's when trying to parse date

I have not had experience with using dates in R. I have read all of the docs but I still can't figure out why I am getting this error. I am trying to take a vector of strings and convert that into a

Show Detail

as.Date giving me NA's

I've tried everything in this thread as.Date returning NA while converting from 'ddmmmyyyy' to try and sort my problem. I'm using these commands to turn a factor into a date: cohort$doi &l...

Show Detail

R as.Date() returns NA consistently

I'm using the John Hopkin's COVID-19 dataset which can be found here: https://data.humdata.org/dataset/novel-coronavirus-2019-ncov-cases namely, the Corona Confirmed Cases Narrow, the Corona Confir...

Show Detail

NA for 1 particular date when converting dates from "character" format to "POSIXct" with as.POSIXct

I'm converting a string vector to date format with as.POSIXct(). Here is the strange thing: as.POSIXct("2017-03-26 03:00:00.000",format="%Y-%m-%d %H") #Gives "2017-03-26 03:00:00 CEST"

Show Detail

Converting Pandas DataFrame dates so that I can pick out particular dates

I have two dataframes with particular data that I'm needing to merge. Date Greenland Antarctica 0 2002.29 0.00 0.00 1 2002.35 68.72 19.01 2 2002.62 -21...

Show Detail

remove rows of dates and duplicate dates for any date that has an NA

I'm looking to remove all dates that have any NAs/missing data for any observation type, from a data.frame that has duplicate dates. For example, here I'd like to end up with a data.frame of just t...

Show Detail

How to get previous 7 dates from a particular date in java?I am getting 7 dates from present date, but I want from particular date

//explain public class DateLoop { static String finalDate; static String particularDate; public static void main(String[] args) { // TODO Auto-generated method stub

Show Detail